<template>

  <div class="product-add">
    <!--form表单-->
    <el-form size="small" ref="form" :model="form" label-width="150px">
      <!--添加门店-->
      <div class="common-form">短信设置</div>
      <el-form-item label="接收手机号">
        <el-input v-model="form.acceptPhone" class="max-w460"></el-input>
        <div class="tips">商家手机号设置（用于接收消息提醒）</div>
      </el-form-item>
      <el-form-item label="默认上传方式">
        <div>
          <el-radio v-model="form.radio" label="Aliyun" @change="getRadio">阿里云短信</el-radio>
          <el-radio v-model="form.radio" label="Qcloud" @change="getRadio">腾讯云短信</el-radio>
          <el-radio v-model="form.radio" label="HWcloud" @change="getRadio">华为云短信</el-radio>
        </div>
      </el-form-item>
      <!--阿里云短信-->
      <div v-if="form.radio == 'Aliyun'">
        <el-form-item label="AccessKey">
          <el-input v-model="form.engine.aliyun.accessKeyId" class="max-w460"></el-input>
        </el-form-item>
        <el-form-item label="AccessKeySecret">
          <el-input v-model="form.engine.aliyun.accessKeySecret" class="max-w460"></el-input>
        </el-form-item>
        <el-form-item label="短信签名">
          <el-input v-model="form.engine.aliyun.sign" class="max-w460"></el-input>
        </el-form-item>
        <el-form-item label="短信模板">
          <el-input v-model="form.engine.aliyun.templateCode" class="max-w460"></el-input>
        </el-form-item>
      </div>

      <!--腾讯云短信-->
      <div v-if="form.radio == 'Qcloud'">
        <el-form-item label="AppId">
          <el-input v-model="form.engine.qcloud.accessKeyId" class="max-w460"></el-input>
        </el-form-item>
        <el-form-item label="SecretId">
          <el-input v-model="form.engine.qcloud.secretId" class="max-w460"></el-input>
        </el-form-item>
        <el-form-item label="SecretKey">
          <el-input v-model="form.engine.qcloud.secretKey" class="max-w460"></el-input>
        </el-form-item>
        <el-form-item label="短信签名">
          <el-input v-model="form.engine.qcloud.sign" class="max-w460"></el-input>
        </el-form-item>
        <el-form-item label="短信模板">
          <el-input v-model="form.engine.qcloud.templateCode" class="max-w460"></el-input>
        </el-form-item>
      </div>

      <!--华为云短信-->
      <div v-if="form.radio == 'HWcloud'">
        <el-form-item label="AccessKey">
          <el-input v-model="form.engine.hwcloud.accessKeyId" class="max-w460"></el-input>
        </el-form-item>
        <el-form-item label="AccessKeySecret">
          <el-input v-model="form.engine.hwcloud.accessKeySecret" class="max-w460"></el-input>
        </el-form-item>
        <el-form-item label="短信通道">
          <el-input v-model="form.engine.hwcloud.sender" class="max-w460"></el-input>
        </el-form-item>
        <el-form-item label="短信签名">
          <el-input v-model="form.engine.hwcloud.sign" class="max-w460"></el-input>
        </el-form-item>
        <el-form-item label="短信模板">
          <el-input v-model="form.engine.hwcloud.templateCode" class="max-w460"></el-input>
        </el-form-item>
        <el-form-item label="APP接入地址">
          <el-input v-model="form.engine.hwcloud.url" class="max-w460"></el-input>
          <div class="tips">请前往华为云短信管理->模板管理->操作->更多->变量校验的APP接入地址</div>
        </el-form-item>
      </div>
      <!--提交-->
      <div class="common-button-wrapper">
        <el-button type="primary" @click="onSubmit" :loading="loading">提交</el-button>
      </div>
    </el-form>


  </div>

</template>

<script>
  import SettingApi from '@/api/setting.js';

  export default {
    data() {
      return {
        /*form表单数据*/
        form: {
          radio: 'aliyun',
          acceptPhone: '',
          engine: {
            aliyun: {},
            qcloud: {},
            hwcloud: {},
          }
        },
        loading: false,
      };
    },
    created() {
      this.getData()
    },

    methods: {
      getData() {
        let self = this;
        SettingApi.smsDetail({}, true)
          .then(res => {
            let vars = res.data;
            self.form.radio = vars.radio;
            self.form.acceptPhone = vars.acceptPhone;
            self.form.engine.aliyun = vars.aliyunSms;
            self.form.engine.qcloud = vars.qcloudSms;
            self.form.engine.hwcloud = vars.hWcloudSms;
          })
          .catch(error => {});

      },
      //提交表单
      onSubmit() {
        let self = this;
        self.loading = true;
        let params = {};
        params.aliyunSms = self.form.engine.aliyun;
        params.qcloudSms = self.form.engine.qcloud;
        params.hWcloudSms = self.form.engine.hwcloud;
        params.radio = self.form.radio;
        params.acceptPhone = self.form.acceptPhone;
        SettingApi.editSms(params, true)
          .then(data => {
            self.loading = false;
            ElMessage ({
              message: '短信通知设置成功',
              type: 'success'
            });

          })
          .catch(error => {
            self.loading = false;
          });
      },
      //发送短信
      sendOut() {
        let self = this;
        let params = this.form;
        SettingApi.sendSms(params, true)
          .then(data => {
            ElMessage ({
              message: '短信发送成功',
              type: 'success'
            });
            self.$router.push('/setting/Sms');

          })
          .catch(error => {

          });
      }



    }

  };
</script>

<style>
  .tips {
    color: #ccc;
  }
</style>
